# タイによるスカラーの監視
package MonScalar;

sub TIESCALAR {
    my ($pkg, $name, $value) = @_;
    my $obj = { name => $name, value => $value };
    bless $obj, $pkg;
    $obj;
}

sub FETCH {
    my $obj = shift;
    print 'Read : $', $obj->{'name'}, " -> $obj->{'value'}\n";
    $obj->{'value'};
}

sub STORE {
    my ($obj, $new_value) = @_;
    print 'Write : $', $obj->{'name'}," <- $new_value\n";
    $obj->{'value'} = $new_value;
}

sub DESTROY {
    print "DESTORY\n";
}


package main;

$x = 10;
tie $x, 'MonScalar', 'x', $x;

$y = $x;
print "y -> $y\n";

$x = 100;
$z = $x;
print "z -> $z\n";

untie $x;

print "x -> $x\n";

# end of file;
